package id.co.rho.scoring.cid.client.helper;

import com.extjs.gxt.ui.client.data.DataProxy;
import com.extjs.gxt.ui.client.data.DataReader;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.requestfactory.shared.Receiver;

/**
 * Created by IntelliJ IDEA.
 * User: User3
 * Date: 07/07/11
 * Time: 16:11
 *
 * @author : Frizky
 * @email : frizkey@gmail.com
 * To change this template use File | Settings | File Templates.
 */
public abstract class RequestFactoryProxy<D> implements DataProxy<D> {
    @Override
    public void load(final DataReader<D> reader, final Object loadConfig,
                     final AsyncCallback<D> callback) {
        load(loadConfig, new Receiver() {
            @Override
            public void onSuccess(Object response) {
                try {
                    D data = null;
                    if (reader != null) {
                        data = reader.read(loadConfig, response);
                    } else {
                        data = (D) response;
                    }
                    callback.onSuccess(data);
                } catch (Exception e) {
                    callback.onFailure(e);
                }
            }
        });
    }

    protected abstract void load(Object loadConfig, Receiver receiver);
}
